package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.CartItem;
import csu.web.mypetstore.domain.Item;
import csu.web.mypetstore.persistence.CartDao;
import csu.web.mypetstore.persistence.DBUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class CartDaoImpl implements CartDao {
    private static final String INSERT_CART_ITEM =
            "INSERT INTO CART (USERNAME,ITEMID,QUANTITY) VALUES (?, ?, 1)";
    private static final String UPDATE_CART_ITEMS =
            "UPDATE CART SET QUANTITY = ? WHERE USERNAME = ? AND ITEMID = ?";
    private static final String REMOVE_CART_ITEM_BY_ID=
            "DELETE FROM CART WHERE ITEMID = ? AND USERNAME = ?";
    private static final String GET_CART_ITEM_BY_USERID=
            "SELECT ITEMID,QUANTITY FROM CART WHERE USERNAME = ?";
    private static final String CONTAINS_CART_ITEM =
            "SELECT QUANTITY FROM CART WHERE USERNAME = ? AND ITEMID = ?";
    private static final String CLEAR_CART_ITEMS =
            "DELETE FROM CART WHERE USERNAME = ?";




    @Override
    public List<CartItem> getItemsByUserID(String username){
        List<CartItem> cartItemList = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_ITEM_BY_USERID);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                CartItem cartItem = new CartItem();
                cartItem.setItemid(resultSet.getString(1));
                cartItem.setQuantity(resultSet.getInt(2));
                cartItemList.add(cartItem);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartItemList;
    }

    //获取物品数量
    public int containsItem(String username,String itemId) {
        int quantity = 0;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(CONTAINS_CART_ITEM);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, itemId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                quantity = resultSet.getInt(1);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return quantity;
    }

    public void insertItem(String username,String itemid)
    {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CART_ITEM);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, itemid);
            //preparedStatement.executeUpdate();
            int rowsAffected=preparedStatement.executeUpdate();
            if(rowsAffected>0){
                System.out.println("添加商品成功");
            }else{
                System.out.println("添加商品失败");
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void removeItemById( String username,String itemid) {
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(REMOVE_CART_ITEM_BY_ID);
            // 设置参数
            preparedStatement.setString(1, itemid);
            preparedStatement.setString(2, username);
            //执行操作
            int rowsAffected=preparedStatement.executeUpdate();
            if(rowsAffected>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override

    public void updateCartItem(String username, String itemId, int quantity) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CART_ITEMS);
            preparedStatement.setInt(1, quantity);
            preparedStatement.setString(2, username);
            preparedStatement.setString(3, itemId);
            preparedStatement.executeUpdate();
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void clearCartItems(String username) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(CLEAR_CART_ITEMS);
            preparedStatement.setString(1, username);
            preparedStatement.executeUpdate();
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }



